//
//  BMAEmotionViewController.m
//  Autismus
//
//  Created by Salhi yacine on 04/12/11.
//  Copyright (c) 2011 EPITECH. All rights reserved.
//

#import "BMAEmotionViewController.h"
#import "SoundManager.h"

#define WIDTH   884 
#define HEIGHT  663
#define ANIMDURATION    1.0

@implementation BMAEmotionViewController

@synthesize backButton;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];    
}

-(IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)pressEmotion:(UIButton *)b
{
    NSString *token = nil;
    
    switch (b.tag) {
        case BMAEmotionEnColere:
            token = @"colere";
            break;

        case BMAEmotionEffraye:
            token = @"effraye";
            break;

        case BMAEmotionJoyeux:
            token = @"joyeux";
            break;
        
        case BMAEmotionTriste:
            token = @"triste";
            break;
            
        case BMAEmotionEtonne:
            token = @"etonne";
            break;
            
        case BMAEmotionMalade:
            token = @"malade";
            break;
    }
    
    if (token)
    {
        [[SoundManager sharedManager] playMusic:[NSString stringWithFormat:@"%@.mp3", token] looping:YES];
        self.view.userInteractionEnabled = NO;
        UIImageView *animViewController = [[UIImageView alloc] init];
        NSArray *animImg = [NSArray arrayWithObjects:
                            [UIImage imageNamed:[NSString stringWithFormat:@"illustration-%@-grand_01.png", token]],
                            [UIImage imageNamed:[NSString stringWithFormat:@"illustration-%@-grand_02.png", token]],
                            [UIImage imageNamed: [NSString stringWithFormat:@"illustration-%@-grand_03.png", token]],
                            nil];
        animViewController.animationImages = animImg;
        animViewController.animationDuration = ANIMDURATION;
        animViewController.animationRepeatCount = 3;
        animViewController.frame = CGRectMake(40, 70, WIDTH, HEIGHT);
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        v.backgroundColor = [UIColor lightGrayColor];
        v.alpha = 0.3;
        [self.view addSubview:v];
        [UIView animateWithDuration:3
                              delay:0
                            options: UIViewAnimationCurveEaseOut
                         animations:^{
                             v.alpha = 0.4;
                         }
                         completion:^(BOOL finished){
                             [[SoundManager sharedManager] stopMusic];
                             self.view.userInteractionEnabled = YES;
                             [v removeFromSuperview];
                         }];
        [self.view addSubview:animViewController];
        [animViewController startAnimating];
    }
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.backButton setImage:[[self.backButton imageForState:UIControlStateNormal] imageTintedWithColor:[UIColor blackColor] fraction:0.5] forState:UIControlStateNormal];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.backButton = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end
